package com.kiloai.base.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import com.kiloai.base.R;


/**
 * Created by david on 17/7/4.
 */

public class ClickLinearLayout extends LinearLayout {

    private float CLICKED_TRANS = 0.8f;
    private final float UNCLICK_TRANS = 1.0f;

    public ClickLinearLayout(Context context) {
        super(context);
    }

    public ClickLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ClickLinearLayout);
        float transparency = typedArray.getFloat(R.styleable.ClickLinearLayout_transparency, 1.0f);
        if (transparency != 1.0f) {
            CLICKED_TRANS = transparency;
        }
    }

    public ClickLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                this.setAlpha(CLICKED_TRANS);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                this.setAlpha(UNCLICK_TRANS);
                break;
        }

        return super.onTouchEvent(event);
    }
}
